<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */
namespace App\Controller\user;

use App\Request\User\Coupon\SearchRequest;
use App\Request\User\Coupon\ListRequest;
use App\Service\User\CouponService;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;

/**
 * 优惠券控制器
 *
 * @AutoController()
 * @author 刘兴永(aile8880@qq.com)
 * @package \App\Controller\user
 */
class CouponController extends Controller
{
    /**
     * 优惠券服务
     *
     * @Inject
     * @var CouponService
     */
    private $service;

    /**
     * 优惠券列表
     *
     * @param ListRequest $request
     */
    public function list(ListRequest $request)
    {
        $params = $request->all();

        isset($params['type']) && $params['type'] = (int)$params['type'];
        isset($params['sort_type']) && $params['sort_type'] = (int)$params['sort_type'];
        isset($params['max_price']) && $params['max_price'] = (float)$params['max_price'];
        isset($params['min_price']) && $params['min_price'] = (float)$params['min_price'];

        $result = $this->service->list($params['type'], $params['sort_type'] ?? 0, $params['max_price'] ?? null, $params['min_price'] ?? null);


        $this->success($result);
    }

    /**
     * 优惠券搜索接口
     *
     * @param SearchRequest $request
     */
    public function search(SearchRequest $request)
    {
        $params = $request->all();

        isset($params['type']) && $params['type'] = (int)$params['type'];
        isset($params['sort_type']) && $params['sort_type'] = (int)$params['sort_type'];
        isset($params['max_price']) && $params['max_price'] = (float)$params['max_price'];
        isset($params['min_price']) && $params['min_price'] = (float)$params['min_price'];

        $result = $this->service->search($params['type'], $params['keywords'], $params['sort_type'] ?? 0, $params['max_price'] ?? null, $params['min_price'] ?? null);

        $this->success($result);
    }

    /**
     * 优惠券详情
     */
    public function CouponDetails()
    {
        $coupon_id = (int)$this->request->input('coupon_id', 0);

        $result =  $this->service->dao->CouponDetails([
            'id' => $coupon_id
        ]);

        $result['rebate'] = filterMoney(($result['price'] / $result['goods_price'])*10,2);

        $result ? $this->success($result): $this->error();
    }

    /**
     * 优惠券确认订单
     *
     */
    public function details()
    {
        $coupon_id = (int)$this->request->input('coupon_id', 0);

        $result =  $this->service->dao->getCouponByMap([
            'id' => $coupon_id
        ]);
        $result->makeHidden('');
        $result ? $this->success($result): $this->error();

    }
}